package com.xiaoshuidi.cloud.module.contract.job;

import com.xiaoshuidi.cloud.framework.tenant.core.job.TenantJob;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.contract.ContractStayDO;
import com.xiaoshuidi.cloud.module.contract.dal.mysql.contract.ContractStayMapper;
import com.xiaoshuidi.cloud.module.contract.service.contract.ContractStayService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 数据迁移
 */

@Component
@Slf4j
@RequiredArgsConstructor
@TenantJob
public class ContractDataMigrationJob {

    private final ContractStayService contractStayService;
    private final ContractStayMapper contractStayMapper;

    @XxlJob("contractStaySetRoomAsset")
    public void execute() {
        List<ContractStayDO> contractStayDOS = contractStayMapper.selectList();
        if (CollectionUtils.isNotEmpty(contractStayDOS)){
            for (ContractStayDO contractStayDO : contractStayDOS) {
                String roomEquipmentsJson = contractStayService.getRoomEquipmentsJson(contractStayDO.getRoomId());
                contractStayDO.setRoomAsset(roomEquipmentsJson);
                contractStayMapper.updateById(contractStayDO);
            }
        }
    }

}
